const helper = {
  inIos: uni.$u.os() === "ios",
  inAndroid: uni.$u.os() === "android",
  queryParams(obj, ...args) {
    for (const key in obj) {
      let val = obj[key];
      if (typeof val == "string") val = encodeURIComponent(val);
      obj[key] = val;
    }
    return uni.$u.queryParams(obj, ...args);
  },
  authorize(scope) {
    return new Promise((success, fail) => {
      uni.authorize({
        scope,
        success,
        fail,
      });
    });
  },
  getStorage(key, defVal = null) {
    return new Promise((resolve) => {
      uni.getStorage({
        key,
        success(res) {
          resolve(res.data);
        },
        fail() {
          resolve(defVal);
        },
      });
    });
  },
  setStorage(key, data) {
    return new Promise((success, fail) => {
      uni.setStorage({
        key,
        data,
        success,
        fail,
      });
    });
  },
  clearStorage() {
    uni.clearStorageSync();
  },
  loading(title = "加载中") {
    uni.showLoading({
      title,
    });
  },
  hideLoading() {
    uni.hideLoading();
  },
  toast(title, icon = "none") {
    if (icon == 1) {
      icon = "success";
    }
    uni.showToast({
      title,
      icon,
    });
  },
  alert(content, opts = {}) {
    return new Promise((resolve) => {
      uni.showModal({
        content,
        showCancel: false,
        ...opts,
        success() {
          resolve();
        },
      });
    });
  },

  confirm(title = "", content, opts) {
    if (!content) content = "";
    else if (typeof content == "object") {
      opts = content;
      if (title.length > 10) {
        content = title;
        title = "";
      } else {
        content = "";
      }
    }
    return new Promise((resolve, reject) => {
      if (opts == "popup") {
        uni.showActionSheet({
          title,
          itemList: [content],
          success: () => {
            resolve();
          },
        });
        return;
      }
      uni.showModal({
        title,
        content,
        ...opts,
        success({
          confirm
        }) {
          if (confirm) resolve();
          else reject();
        },
      });
    });
  },
  showImg(current, urls, opts = {}) {
    let indicator = "number";
    if (typeof current == "string") {
      if (Array.isArray(urls)) {
        current = urls.indexOf(current);
      } else {
        urls = [current];
        current = 0;
        indicator = "none";
      }
    }
    uni.previewImage({
      current,
      urls,
      loop: true,
      indicator,
      ...opts,
    });
  },
  setTitle(title) {
    return uni.setNavigationBarTitle({
      title,
    });
  },
  sleep(msec) {
    return new Promise((resolve) => {
      setTimeout(resolve, msec);
    });
  },
  getRect(page, id) {
    return new Promise((resolve) => {
      uni
        .createSelectorQuery()
        .in(page)
        .select(id)
        .boundingClientRect(resolve)
        .exec();
    });
  },
  scrollToTop(scrollTop = 0, duration = 0) {
    uni.pageScrollTo({
      scrollTop,
      duration,
    });
  },
  setClipboard(data, msg = "已复制") {
    uni.setClipboardData({
      data,
      success: () => {
        if (msg) {
          uni.showToast({
            title: msg,
            icon: "none",
          });
        }
      },
    });
  },
  async getClipboard() {
    // #ifdef H5
    if (Date) return "h5不支持";
    // #endif
    return new Promise((resolve) => {
      uni.getClipboardData({
        success(res) {
          resolve(res.data);
        },
        fail() {
          resolve("");
        },
      });
    });
  },
  share(opts = {}, scene = "WXSceneSession") {
    return new Promise((resolve, reject) => {
      uni.share({
        provider: "weixin",
        scene,
        type: 0,
        ...opts,
        success: resolve,
        fail: reject,
      });
    });
  },
  scanCode() {
    // #ifdef H5
    if (Date)
      return {
        result: "tablo.fun hqs.link",
      };
    // #endif
    return new Promise((success, fail) => {
      uni.scanCode({
        success,
        fail,
      });
    });
  },
  openLocation(info, scale = 14) {
    const {
      name,
      addr,
      lat,
      lon
    } = info;
    uni.openLocation({
      name,
      latitude: lat,
      longitude: lon,
      scale,
      address: addr,
    });
  },
};

helper.onAuthFail = (msg = "无保存相册权限") => {
  helper
    .confirm(msg, {
      confirmText: "去设置",
    })
    .then(() => {
      helper.openSetting();
    });
};

export default helper;